#include "build_cmd.h"

static int scsi_tur_cmd_done(scsi_cmd_t *cmd)
{
    if (cmd->exec_args.sshdr == NULL) {
        return -1;
    }
    printk("response_code=%u\n", cmd->exec_args.sshdr->response_code);
    printk("sense_key=%u\n", cmd->exec_args.sshdr->sense_key);
    printk("asc=%u\n", cmd->exec_args.sshdr->asc);
    printk("ascq=%u\n", cmd->exec_args.sshdr->ascq);
    return 0;
}

void build_scsi_tur_cmd(scsi_cmd_t *cmd)
{
    cmd->callback = scsi_tur_cmd_done;
    cmd->scsi_cmd[0] = TEST_UNIT_READY;
}

static int scsi_inquiry_cmd_done(scsi_cmd_t *cmd)
{
    unsigned char peripheral_qualifier = 0;
    unsigned char peripheral_type = 0;
    int scsi_version = 0;
    int response_len = 0;
    char product[17] = {0};

    if (cmd->data_out_buffer == NULL) {
        return -1;
    }
    peripheral_qualifier = (*cmd->data_out_buffer) & 0xe0;
    peripheral_type = (*cmd->data_out_buffer) & 0x1f;
    scsi_version = (int)(*(cmd->data_out_buffer + 2));
    response_len = (int)(*(cmd->data_out_buffer + 4)) + 5;
    memcpy(product, cmd->data_out_buffer + 16, 16);
    product[16] = '\0';

    printk("peripheral_qualifier=%u\n", peripheral_qualifier);
    printk("peripheral_type=%u\n", peripheral_type);
    printk("scsi_version=%d\n", scsi_version);
    printk("response_len=%d\n", response_len);
    printk("product=%s\n", product);
    return 0;
}

void build_scsi_inquiry_cmd(scsi_cmd_t *cmd)
{
    cmd->data_out_buffer_len = 36;
    cmd->callback = scsi_inquiry_cmd_done;
    cmd->scsi_cmd[0] = INQUIRY;
    cmd->scsi_cmd[4] = (unsigned char)cmd->data_out_buffer_len;
}

static int scsi_inquiry_0x00_cmd_done(scsi_cmd_t *cmd)
{
    int page_len = 0;
    if (cmd->data_out_buffer == NULL) {
        return -1;
    }
    page_len = (int)(*(cmd->data_out_buffer + 3));
    printk("page_len=%d\n", page_len);
    return 0;
}

void build_scsi_inquiry_0x00_cmd(scsi_cmd_t *cmd)
{
    cmd->data_out_buffer_len = 36;
    cmd->callback = scsi_inquiry_0x00_cmd_done;
    cmd->scsi_cmd[0] = INQUIRY;
    cmd->scsi_cmd[1] = 0x01;
    cmd->scsi_cmd[2] = 0x00;
    cmd->scsi_cmd[4] = (unsigned char)cmd->data_out_buffer_len;
}

static int scsi_inquiry_0x80_cmd_done(scsi_cmd_t *cmd)
{
    int page_len = 0;

    if (cmd->data_out_buffer == NULL) {
        return -1;
    }
    page_len = (int)(*(cmd->data_out_buffer + 3));
    printk("page_len=%d\n", page_len);
    return 0;
}

void build_scsi_inquiry_0x80_cmd(scsi_cmd_t *cmd)
{
    cmd->data_out_buffer_len = 36;
    cmd->callback = scsi_inquiry_0x80_cmd_done;
    cmd->scsi_cmd[0] = INQUIRY;
    cmd->scsi_cmd[1] = 0x80;
    cmd->scsi_cmd[2] = 0x80;
    cmd->scsi_cmd[4] = (unsigned char)cmd->data_out_buffer_len;
}

static int scsi_inquiry_0x83_cmd_done(scsi_cmd_t *cmd)
{
    int page_len = 0;
    if (cmd->data_out_buffer == NULL) {
        return -1;
    }
    page_len = (int)(*(cmd->data_out_buffer + 3));
    printk("page_len=%d\n", page_len);
    return 0;
}

void build_scsi_inquiry_0x83_cmd(scsi_cmd_t *cmd)
{
    cmd->data_out_buffer_len = 36;
    cmd->callback = scsi_inquiry_0x83_cmd_done;
    cmd->scsi_cmd[0] = INQUIRY;
    cmd->scsi_cmd[1] = 0x01;
    cmd->scsi_cmd[2] = 0x83;
    cmd->scsi_cmd[4] = (unsigned char)cmd->data_out_buffer_len;
}
